//	 Copyright 2011 Elian ORIOU
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//       http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.

package org.eo.geomapper;

import java.io.IOException;
import java.util.List;

import org.eo.geomapper.objects.ILayer;
import org.eo.geomapper.parser.GeoMapperParser;
import org.eo.geomapper.renderer.GeoMapperRenderer;

/**
 * The GeoMapper Main Class
 * 
 * @author Elian ORIOU
 * 
 */

public class GeoMapper {

	private GeoMapperParser parser;
	private GeoMapperRenderer renderer;
	private List<ILayer> layers;

	public GeoMapper() {
		this.parser = new GeoMapperParser();
		this.renderer = new GeoMapperRenderer();
	}

	/**
	 * Loads GIS informations from XML file
	 * 
	 * @param xmlFilePath
	 *            The informations XML file
	 * @return true if the loading is a success, false otherwise
	 */

	public boolean loadGeographicalInformations(String xmlFilePath) {
		try {
			this.layers = this.parser.parse(xmlFilePath);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return !this.layers.isEmpty();
	}

	/**
	 * Saves the image sized by width and height to the specified filePath
	 * 
	 * @param width
	 *            The generated image width
	 * @param height
	 *            The generated image height
	 * @param filePath
	 *            The generated image file path
	 * @throws IOException
	 */

	public void saveImage(int width, int height, String filePath)
			throws IOException {
		if (this.layers == null) {
			return;
		}
		String extension = filePath.substring(filePath.lastIndexOf('.') + 1);
		renderer.generateImage(this.layers, width, height);
		renderer.saveImage(filePath, extension);
	}
}
